package com.zsix.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zsix.common.entity.taskAssignee.ApprovalTrack;
import com.zsix.system.mapper.processMapper.ProcessManageMapper;
import com.zsix.system.mapper.taskAssigneeMapper.ApprovalTrackMapper;
import com.zsix.system.mapper.taskAssigneeMapper.FlowElementAttrsMapper;
import com.zsix.system.mapper.taskAssigneeMapper.GenerWorkMapper;
import com.zsix.system.mapper.taskAssigneeMapper.OvTaskEntityMapper;
import com.zsix.system.service.IProcessManageService;
import com.zsix.system.service.manager.ProcessMManager;
import com.zsix.common.dto.controllerParams.StatAnalysisParams;
import com.zsix.common.entity.process.ProcessManage;
import com.zsix.common.entity.taskAssignee.FlowElementAttrs;
import com.zsix.common.entity.taskAssignee.GenerWork;
import com.zsix.common.entity.taskAssignee.OvTaskEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class ProcessManageServiceImpl implements IProcessManageService {
    @Autowired
    private ProcessManageMapper processManageMapper;
    @Autowired
    private ApprovalTrackMapper approvalTrackMapper;
    @Autowired
    private ProcessMManager processMManager;
    @Autowired
    private GenerWorkMapper generWorkMapper;
    @Autowired
    private FlowElementAttrsMapper flowElementAttrsMapper;
    @Autowired //自定义
    private OvTaskEntityMapper ovTaskEntityMapper;

    @Override
    public List<ProcessManage> getProcessManageList(String title, String processKey, String state){
        return processManageMapper.processManageList(title,processKey,state);
    }
    /**
     * 获取已经启动的流程实例的分页数据
     * @param processManage
     * @return
     */
    @Override
    public List<ProcessManage> getProcessManageListPage(ProcessManage processManage){
        List<ProcessManage> processManages=processManageMapper.processManageList(processManage.getTitle(),
                processManage.getName_(), processManage.getState());
        return processManages;
    }
    /**
     * 获取流程实例补充参数
     * @param processManage
     * @return
     */
    @Override
    public HashMap<String,Object> getManageParams(ProcessManage processManage){
        HashMap<String,Object> hashMap=new HashMap<>();
        List<ApprovalTrack> tracks=approvalTrackMapper.approvalTracks(processManage.getProc_inst_id_());
        List<OvTaskEntity> tasks = ovTaskEntityMapper.ovTaskEntityByProcessInsId(processManage.getProc_inst_id_());
        GenerWork generWork=generWorkMapper.getGenerWorkByInst(processManage.getProc_inst_id_());
        if(generWork==null)
            generWork=generWorkMapper.generWork(processManage.getBusiness_key_());
        if(tasks.size()>0) {
            OvTaskEntity task = tasks.get(0);
            FlowElementAttrs flowElementAttrs=
                    flowElementAttrsMapper.getFlowElementAttrs(task.getTask_def_key_());
            hashMap.put("form_type",flowElementAttrs.getForm_type());
            hashMap.put("form_url",flowElementAttrs.getForm_url());
            hashMap.put("taskid",task.getId_());
            hashMap.put("map_json",flowElementAttrs.getMapJson());
            hashMap.put("task_def_name",task.getName_());
        }
        else{
            try{
                if(tracks.size()>0){
                    ApprovalTrack track=tracks.get(0);
                    FlowElementAttrs flowElementAttrs=
                            flowElementAttrsMapper.getFlowElementAttrs(track.getUserTaskId());
                    hashMap.put("form_type",flowElementAttrs.getForm_type());
                    hashMap.put("form_url",flowElementAttrs.getForm_url());
                    hashMap.put("map_json",flowElementAttrs.getMapJson());
                    //hashMap.put("taskid",track.g);
                    hashMap.put("task_def_name",track.getUserTaskName());
                }
                else {
                    hashMap.put("form_type", "0");
                    hashMap.put("form_url", "");
                }
            }
            catch (Exception ex){
                hashMap.put("form_type","1");
                hashMap.put("form_url",generWork.getFormMap());
            }
            hashMap.put("taskid",'0');
            hashMap.put("task_def_name","结束");
        }
        //hashMap.put("map_json",generWork.getFormMap());
        hashMap.put("data_json",generWork.getData_json());
        return hashMap;
    }

    /**
     * 获取所有任务分页
     * @param processManage
     * @return
     */
    @Override
    public List<GenerWork> getTaskManageListPage(ProcessManage processManage){
        List<GenerWork> generWorks=processManageMapper
                .getTaskManageList(processManage.getTitle(),processManage.getUserid());
        return generWorks;
    }

    /**
     * 获取流程统计分析数据
     * @param statAnalysisParams
     * @return
     */
    @Override
    public HashMap<String,Object> getStatAnalysisData(StatAnalysisParams statAnalysisParams){
        HashMap<String,Object> hashMap=processMManager.getStatAnalysisData(statAnalysisParams);
        return hashMap;
    }

    /**
     * 获取当前系统登陆人发起的流程列表
     * @param params
     * @return
     */
    @Override
    public HashMap<String,Object> getMyStartListPage(JSONObject params){
        String title=params.getString("title");
        String flowName=params.getString("flowName");
        Integer page=params.getInteger("page");
        Integer pagesize=params.getInteger("pageSize");
        String state=params.getString("state");
        return processMManager.getMyStartList(title,flowName,state,page,pagesize);
    }

    /**
     * 获取我审核的数据
     * @param params
     * @return
     */
    @Override
    public HashMap<String,Object> getMyProcessPage(JSONObject params){
        String title=params.getString("title");
        String flowName=params.getString("flowName");
        Integer page=params.getInteger("page");
        Integer pagesize=params.getInteger("pageSize");
        String state=params.getString("state");
        return processMManager.getMyProcessPage(title,flowName,state,page,pagesize);
    }
}
